﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using CodeGenerator.Item;
using CodeGenerator.utils;
using Microsoft.WindowsAPICodePack.Dialogs;
using hc = HandyControl.Controls;

namespace CodeGenerator {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        private List<string> list = new List<string>();
        private ObservableCollection<CustomItem> extensions = new ObservableCollection<CustomItem>();

        public MainWindow() {
            InitializeComponent();

            //initData();
        }

        public void initData() {
            list.Add(@"D:\Codes\Projects\软著代码整理\AnomalyDetectionServer");
            list.Add(@"D:\Codes\Projects\软著代码整理\AnomalyDetetion");

            extensions.Add(new CustomItem() { Item = "cs" });
            extensions.Add(new CustomItem() { Item = "py" });
            FileNames.ItemsSource = list;
            FileExtensions.ItemsSource = extensions;

            TextBoxOutputFileName.Text = @"D:\Codes\Projects\软著代码整理\word\11.docx";
        }

        private void AddFiles_Click(object sender, RoutedEventArgs e) {
            // 弹出文件夹选择框
            var dialog = new CommonOpenFileDialog() {
                IsFolderPicker = true,
                Multiselect = true,
                Title = "选择文件夹",
            };
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok) {
                foreach (var item in dialog.FileNames) {
                    list.Add(item);
                }
            }

            Console.WriteLine(list.Count);
            FileNames.ItemsSource = null;
            FileNames.ItemsSource = list;
            FileNames.InvalidateVisual();
        }

        private void DeleteFiles_Click(object sender, RoutedEventArgs e) {
            // 删除选中的文件
            var selected = FileNames.SelectedItems;
            foreach (var item in selected) {
                list.Remove(item.ToString());
            }

            FileNames.ItemsSource = null;
            FileNames.ItemsSource = list;
            FileNames.InvalidateVisual();
        }

        private void AddExtensions_Click(object sender, RoutedEventArgs e) {
            Console.WriteLine($"extensions.Count = {extensions.Count}, 内容:");
            foreach (var extension in extensions) {
                Console.WriteLine(extension.Item);
            }

            // 添加扩展名
            extensions.Add(new CustomItem());
            FileExtensions.ItemsSource = null;
            FileExtensions.ItemsSource = extensions;
        }

        private void DeleteExtensions_Click(object sender, RoutedEventArgs e) {
            // 删除选中的扩展名
            var selected = FileExtensions.SelectedItems;

            selected.Cast<CustomItem>().ToList().ForEach(item => { extensions.Remove(item); });

            FileExtensions.ItemsSource = null;
            FileExtensions.ItemsSource = extensions;
        }

        private void ButtonSaveName_Click(object sender, RoutedEventArgs e) {
            // 弹出文件保存选择框
            var dialog = new SaveFileDialog() {
                Filter = "word文件|*.docx",
                Title = "保存文件名",
            };

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                TextBoxOutputFileName.Text = dialog.FileName;
            }
        }

        private void ButtonGenerate_Click(object sender, RoutedEventArgs e) {
            // 
            Tools.MainWindow = this;
            Tools.files.Clear();
            var filenames = list.ToArray();
            var extensions = this.extensions.Select(item => item.Item).ToArray();
            var outputFileName = TextBoxOutputFileName.Text;

            if (filenames.Length == 0) {
                hc.Growl.Warning("请选择文件夹");
                return;
            }

            if (extensions.Length == 0) {
                hc.Growl.Warning("请选择扩展名");
                return;
            }

            if (outputFileName == "") {
                hc.Growl.Warning("请选择输出文件名");
                return;
            }
            ButtonGenerate.IsEnabled = false;
            Task.Factory.StartNew(() => {
                Tools.GetAllFiles(filenames, extensions);
                var filesCount = Tools.files.Count;
                // Dispatcher.Invoke(() => { hc.Growl.Info($"一共{filesCount}个文件, 开始读取..."); });

                var lines = Tools.ReadAllLines();
                Console.WriteLine(lines.Count);
                Tools.GenerateDocx(lines, outputFileName);

                // 获取扩展名
                var txtFileName = outputFileName.Substring(0, outputFileName.LastIndexOf(".")) + ".txt";
                Console.WriteLine(txtFileName);
                File.WriteAllLines(txtFileName, lines);

                Dispatcher.Invoke(() => {
                    hc.Growl.Info($"生成成功!, 文件已保存在{outputFileName}");
                    ButtonGenerate.IsEnabled = true;
                });
            });
        }
    }
}